package ualrcdg.shared.dto;

import java.util.ArrayList;
import java.util.HashMap;

import ualrcdg.shared.exceptions.CDGException;

/**
 * Simple implementation of the "character" object. See CDG XML Specifications: Game Descriptor Files.
 */
public class Character
{
	private final String id;
	private final HashMap< String, SimpleProperty< ? > > properties;
	
	/**
	 * Primary constructor
	 *
	 * @param id the id of the Character object to be created
	 * @param properties the properties of the Character object to be created
	 * @throws CDGException If the Character cannot be created with the initial member values provides
	 */
	public Character( String id, ArrayList< SimpleProperty<?> > properties ) throws CDGException
	{
		if( id == null || properties == null )
		{
			throw new CDGException( "Character constructor called with one or more null arguments:" + "Character [id=" + id + ", properties=" + properties + "]" );
		}
		
		this.id = id;
		this.properties = new HashMap< String, SimpleProperty< ? > >( );
		for( SimpleProperty< ? > p : properties )
		{
			SimpleProperty< ? > pCopy = new SimpleProperty( p );
			if( this.properties.put( pCopy.getName( ), pCopy ) != null )
			{
				throw new CDGException( "Cannot create Character. Duplicate property name: " + pCopy.getName( ) );
			}
		}
		
		System.out.println( "Parsed: " + this.toString( ) );
	}
	
	/*
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString( )
	{
		return "Character [id=" + id + ", properties=" + properties + "]";
	}

	/**
	 * @return the id of this Character instance
	 */
	public String getId( )
	{
		return id;
	}
	
	/**
	 * Returns the value this Character instance's specified property
	 * 
	 * @param propertyName the name of the property to be retrieved
	 * @return the value of the desired property
	 * @throws CDGException If the propertyName parameter does not identify one of this Character instance's properties 
	 */
	public Object getProperty( String propertyName ) throws CDGException
	{
		SimpleProperty<?> p = this.properties.get( propertyName );
		
		if( p == null )
		{
			throw new CDGException( "Could not locate property with name: " + propertyName );
		}
		
		return p.getValue( );
	}	
}
